﻿using System;
using System.Collections.Generic;
using System.Text;
using JWFW.SFL;
using JWFW.Encrypt;

namespace JWFW.Model.Register
{
    /// <summary>
    /// 单机注册类：使用机器码注册
    /// </summary>
    public class PCRegister
    {
        //防调试

        /// <summary>
        /// Base64加密类1
        /// </summary>
        protected Base64 g_b64 = new Base64();

        #region 获取机器码

        /// <summary>
        /// 获取机器码：
        /// 流程：
        /// 1.获取硬盘序列号并补足到12位，同时存储在字典中备用
        /// 2.获取MAC并补足到12位，同时存储在字典中备用
        /// 3.硬盘序列号从前，MAC从后逐个字符叠加，并映射到A-Z之间
        /// 字典中备用的硬盘序列号以及MAC用于其他地方进行验证
        /// </summary>
        /// <returns>客户电脑的机器码</returns>
        public string GetMC()
        {
            try
            {
                string hdId = EnvironmentOP.GetHDInfo(0);
                string mac = EnvironmentOP.GetMacAddress().Replace(":", "");
                hdId = FillString(hdId, 12);
                mac = FillString(mac, 12);
                return g_b64.Encode(CreateMC(hdId, mac));
            }
            finally
            {
            }
        }
        /// <summary>
        /// 补足字符串长度
        /// </summary>
        /// <param name="origin">待补足长度的原始字符串</param>
        /// <param name="length">需要补足到的长度</param>
        /// <returns>补足后的字符串</returns>
        protected string FillString(string origin, int length)
        {
            string table = "DA12B4C35EF6";
            int delta = length - origin.Length;
            if (delta < 0)
            {
                return origin.Substring(0, length);
            }
            for (int i = 0; i < delta; i++)
            {
                int tmp = CommonOP.GetStringSum(origin, 0, origin.Length);
                tmp = tmp * 7 % table.Length;
                origin = origin + table[tmp].ToString();
            }
            return origin;
        }
        /// <summary>
        /// 创建机器码
        /// </summary>
        /// <param name="hdId">硬盘序列号</param>
        /// <param name="mac">网卡地址</param>
        /// <returns>交叉运算后的标识客户电脑的机器码</returns>
        protected string CreateMC(string hdId, string mac)
        {
            string res = "";
            for (int i = 0, j = hdId.Length - 1; i < hdId.Length; i++, j--)
            {
                int tmp = char.ConvertToUtf32(hdId, i) + char.ConvertToUtf32(mac, j);
                tmp = tmp % 26 + 65;
                res = res + char.ConvertFromUtf32(tmp);
            }
            return res;
        }
        /// <summary>
        /// 获得单机注册的机器码
        /// </summary>
        /// <param name="toolCode">工具的代码</param>
        /// <returns>用于单机注册的机器码</returns>
        public string GetPCMC(string toolCode)
        {
            string res = "";
            string mc = g_b64.Decode(this.GetMC());
            for (int i = 0; i < mc.Length; i++)
            {
                res += mc[i] + (toolCode.Length > i ? toolCode[i].ToString() : "");
            }
            return g_b64.Encode(res);
        }

        #endregion

        private void Test()
        {
            DateTime start = DateTime.Now;
            string a = "aaa";
            DateTime end = DateTime.Now;
            if ((end - start).TotalSeconds > 1)
            {
                throw new Exception("");
            }
            string b = a;
            Console.WriteLine(b);
        }
    }
}
